package presentation.windowsFackData;

import com.cloudgarden.layout.AnchorConstraint;
import com.cloudgarden.layout.AnchorLayout;
import com.toedter.calendar.JDateChooser;

import java.awt.Dimension;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;



import util.DateTools;
import util.Log;
import util.NotFoundException;
import util.Subsystem;
import domain.user.UnpermittedActionException;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * This is the screen for writing a new fake post to the forum
 */
public class AddPostFakeFrame extends javax.swing.JFrame
{

	private static final long serialVersionUID = 7599438426143909194L ;
	private JTextArea postText;
	private JButton okPost;
	private JLabel addLable;
	private JSpinner hourSpinner;
	private JPanel datePanel;
	private JScrollPane jScrollPane1;
	private JButton cancelPost;
	private JDateChooser dateOfPost;

	ViewThreadFakePanel threadPanel;

	/**
	 * Constructor
	 * @param viewThreadPanel - the thread to which the post is written
	 */
	public AddPostFakeFrame(final ViewThreadFakePanel viewThreadPanel)
	{
		this.setPreferredSize(new Dimension(735, 566));
		this.threadPanel = viewThreadPanel;
		this.initGUI();
	}

	/**
	 * Initialize the addPost screen
	 */
	public void initGUI()
	{
		
		AnchorLayout thisLayout = new AnchorLayout();
		getContentPane().setLayout(thisLayout);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setTitle("Adding new message");
		{
	
			this.hourSpinner = new JSpinner((new SpinnerNumberModel(10, 0, 23, 1)));
			getContentPane().add(this.hourSpinner, new AnchorConstraint(875, 860, 905, 736, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.hourSpinner.setPreferredSize(new java.awt.Dimension(90, 16));
		}
		{
			this.datePanel = new JPanel();
			getContentPane().add(this.datePanel, new AnchorConstraint(869, 681, 905, 522, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
			this.datePanel.setPreferredSize(new java.awt.Dimension(116, 19));
		}

		this.postText = new JTextArea();
		
		this.jScrollPane1 = new JScrollPane();
		getContentPane().add(this.jScrollPane1, new AnchorConstraint(126, 918, 846, 26, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));
		this.jScrollPane1.setPreferredSize(new java.awt.Dimension(648, 383));
		this.jScrollPane1.setViewportView(this.postText);

		this.okPost = new JButton();
		this.okPost.setText("OK");
		this.okPost.setPreferredSize(new java.awt.Dimension(73, 34));
		getContentPane().add(this.okPost, new AnchorConstraint(869, 222, 933, 121, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

		this.okPost.addActionListener(new java.awt.event.ActionListener() {
	         @SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
	        	 AddPostFakeFrame.this.okButtonHandler();
	         }
		 });

		this.cancelPost = new JButton();
		this.cancelPost.setText("cancel");
		this.cancelPost.setPreferredSize(new java.awt.Dimension(75, 34));
		getContentPane().add(this.cancelPost, new AnchorConstraint(869, 403, 933, 300, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

		this.cancelPost.addActionListener(new java.awt.event.ActionListener() {
	         @SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
	        	 AddPostFakeFrame.this.cancelButtonHandler();
	         }
		 });

		this.addLable = new JLabel();
		this.addLable.setText("enter your message here:");
		this.addLable.setPreferredSize(new java.awt.Dimension(207, 14));
		getContentPane().add(this.addLable, new AnchorConstraint(78, 311, 104, 26, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL, AnchorConstraint.ANCHOR_REL));

		this.dateOfPost = new JDateChooser();
		this.datePanel.add(this.dateOfPost);
		this.dateOfPost.setPreferredSize(new java.awt.Dimension(122, 16));
	}

	private void okButtonHandler()
	{		
		new SwingWorker<Void,Void>(){	
			@SuppressWarnings("synthetic-access")
			@Override
			protected Void doInBackground(){
				String messageText = AddPostFakeFrame.this.postText.getText();
				if(messageText==null || messageText.equals(""))
				{
					//no message insertion:
					JOptionPane.showMessageDialog(AddPostFakeFrame.this,
						"please enter a message", "error occured", JOptionPane.ERROR_MESSAGE);
				}
				
				else{
		
					try
					{
						Date postDate = DateTools.set(DateTools.set(DateTools.set(AddPostFakeFrame.this.dateOfPost.getDate(), Calendar.SECOND, 0), Calendar.MINUTE, 0), Calendar.HOUR_OF_DAY, (Integer)AddPostFakeFrame.this.hourSpinner.getValue()) ;
						AddPostFakeFrame.this.threadPanel.addPost(messageText, postDate);
						AddPostFakeFrame.this.dispose(); //close the window
					}
					catch (RemoteException e)
					{
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("communication problem when trying to add message.") ;
						//show error message:
						JOptionPane.showMessageDialog(AddPostFakeFrame.this,
							"problem with adding the message", "error occured", JOptionPane.ERROR_MESSAGE);
					}
					catch (UnpermittedActionException e)
					{
						long userId = AddPostFakeFrame.this.threadPanel.getSharedData().getUser().getId();
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("user "+userId+" is unpermitted to add message.") ;
						//show error message:
						JOptionPane.showMessageDialog(AddPostFakeFrame.this,
							"you are unpermitted to add new messages", "error occured", JOptionPane.ERROR_MESSAGE);
					}
					catch (NotFoundException e)
					{
						Log.getLogger(Subsystem.PRESENTATION_MODEL).info("the wanted thread to add message to could not be found.") ;
						//show error message:
						JOptionPane.showMessageDialog(AddPostFakeFrame.this,
							"problem with adding the message", "error occured", JOptionPane.ERROR_MESSAGE);
					}
				}
				return null;
			}
			@Override
			protected void done(){/*nothing to do here */}
		}.execute();
	}

	private void cancelButtonHandler()
	{
		this.dispose(); //close the window
	}
	
	/**
	 * @see java.awt.Window#dispose()
	 */
	@Override
	public void dispose()
	{
		super.dispose() ;
		this.threadPanel.removeOpenSubFrame(this);
	}


}

